package com.service.impl;

import com.entity.PropertyMonthlyFinance;
import com.entity.PropertyProjectInfo;
import com.repository.PropertyMonthlyFinanceRepository;
import com.repository.PropertyProjectInfoRepository;
import com.service.PropertyMonthlyFinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class PropertyMonthlyFinanceServiceImpl implements PropertyMonthlyFinanceService {

    @Autowired
    private PropertyMonthlyFinanceRepository propertyMonthlyFinanceRepository;

    @Autowired
    private PropertyProjectInfoRepository propertyProjectInfoRepository;

    @Override
    public PropertyMonthlyFinance save(PropertyMonthlyFinance propertyMonthlyFinance) {
        propertyMonthlyFinance.setBalance(propertyMonthlyFinance.getIncomeAmount().subtract(propertyMonthlyFinance.getExpenseAmount()));
        PropertyMonthlyFinance save = propertyMonthlyFinanceRepository.save(propertyMonthlyFinance);
        updateProjectInfo(propertyMonthlyFinance.getProjectName());
        return save;
    }

    private void updateProjectInfo(String projectName) {
        List<PropertyMonthlyFinance> list = propertyMonthlyFinanceRepository.findAllByProjectName(projectName);
        BigDecimal totalBalance = list.stream()
                .map(PropertyMonthlyFinance::getBalance)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        PropertyProjectInfo propertyProjectInfo = propertyProjectInfoRepository.findByProjectName(projectName);
        propertyProjectInfo.setActualAmount(totalBalance);
        propertyProjectInfoRepository.save(propertyProjectInfo);
    }


    @Override
    public void deleteById(Integer id) {
        PropertyMonthlyFinance propertyMonthlyFinance = findById(id);
        propertyMonthlyFinanceRepository.deleteById(id);
        updateProjectInfo(propertyMonthlyFinance.getProjectName());
    }

    @Override
    public PropertyMonthlyFinance update(PropertyMonthlyFinance propertyMonthlyFinance) {
        propertyMonthlyFinance.setBalance(propertyMonthlyFinance.getIncomeAmount().subtract(propertyMonthlyFinance.getExpenseAmount()));
        PropertyMonthlyFinance save = propertyMonthlyFinanceRepository.save(propertyMonthlyFinance);
        updateProjectInfo(propertyMonthlyFinance.getProjectName());
        return save;
    }

    @Override
    @Transactional(readOnly = true)
    public PropertyMonthlyFinance findById(Integer id) {
        return propertyMonthlyFinanceRepository.findById(id).orElse(null);
    }

    @Override
    @Transactional(readOnly = true)
    public List<PropertyMonthlyFinance> findAll() {
        return propertyMonthlyFinanceRepository.findAll();
    }

    @Override
    @Transactional(readOnly = true)
    public Page<PropertyMonthlyFinance> findAll(Pageable pageable) {
        return propertyMonthlyFinanceRepository.findAll(pageable);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<PropertyMonthlyFinance> findByProjectNameContaining(String projectName, Pageable pageable) {
        return propertyMonthlyFinanceRepository.findByProjectNameContaining(projectName, pageable);
    }
}